package org.bim.view.drag.design.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bim.view.drag.design.api.vo.TProjectShareVO;
import org.bim.view.drag.design.api.vo.TUserVO;
import org.bim.view.drag.design.dao.ProjectMapper;
import org.bim.view.drag.design.dao.ProjectShareMapper;
import org.bim.view.drag.design.dao.UserMapper;
import org.bim.view.drag.design.entity.TProject;
import org.bim.view.drag.design.entity.TProjectShare;
import org.bim.view.drag.design.entity.TUser;
import org.bim.view.drag.design.exception.BizCommonException;
import org.bim.view.drag.design.model.HttpResult;
import org.bim.view.drag.design.util.AssertUtil;
import org.bim.view.drag.design.util.ThreadLocalUtil;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Slf4j
@Api(tags = "项目成员管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("project/members")
public class ProjectMemberController {
    private final ProjectShareMapper projectShareMapper;
    private final ProjectMapper projectMapper;
    private final UserMapper userMapper;
    @GetMapping()
    public HttpResult<List<TProjectShareVO>> memberList(@ApiParam(required = true) @RequestParam String projectId){
        List<TProjectShareVO> result =new ArrayList<>();
       TProject project =  projectMapper.selectByPrimaryKey(projectId);
       AssertUtil.isTrue(project!=null,new BizCommonException("项目不存在"));
       TUser user = userMapper.selectByPrimaryKey(project.getOwnerId());
        TProjectShareVO owner =new TProjectShareVO();
        owner.setProjectId(projectId);
        owner.setIsOwner(true);
        owner.setShareId(user.getId());
        owner.setShareUserName(user.getName());
        result.add(owner);
        List<TProjectShareVO> list = projectShareMapper.selectByProjectId(projectId);
        result.addAll(list);
        return HttpResult.success(result);
    }
    @PostMapping
    public HttpResult addMember(@Valid @RequestBody TProjectShare share){
        TProject project =  projectMapper.selectByPrimaryKey(share.getProjectId());
        AssertUtil.isTrue(project!=null,new BizCommonException("项目不存在"));
        Example example =new Example(TProjectShare.class);
        example.createCriteria()
                .andEqualTo("projectId",share.getProjectId())
                .andEqualTo("shareId", share.getShareId());
        TProjectShare projectShare =  projectShareMapper.selectOneByExample(example);
        if(projectShare==null){
            projectShareMapper.insertSelective(share);
        }else {
            projectShare.setShareRole(share.getShareRole());
            projectShareMapper.updateByPrimaryKeySelective(projectShare);
        }
        return HttpResult.success();
    }
    @DeleteMapping("{id}")
    public HttpResult removeMember(@PathVariable String id){
        TUserVO currentUser = ThreadLocalUtil.getCurrentUser();
        TProjectShare share = projectShareMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(share!=null,new BizCommonException("成员Id不对"));
        TProject project =  projectMapper.selectByPrimaryKey(share.getProjectId());
        boolean flag = Objects.equals(currentUser.getId(),project.getOwnerId());
        AssertUtil.isTrue(flag,new BizCommonException("无权删除成员",403));
        return HttpResult.success();
    }
}
